<?php 


function User_Reg_notify_url($out_trade_no){
    global $conf,$date;
    $get = input("get");
    $sql = "select * from `pay_regcode` where `trade_no`='{$out_trade_no}' order by id desc limit 1";
    $row = executeSQLQuery($sql);
    if($get['pid'] == $conf['zero_pid'] && $row['status']==0){
        $user = explode('|',$row['data'])[1];
		$pass = explode('|',$row['data'])[2];
		$email = explode('|',$row['data'])[3];
		$qq = explode('|',$row['data'])[4];
		$phone = explode('|',$row['data'])[5];
		$pid='1'.mt_rand(10000000,99999999);
		$key = md5(random(11));
		$money =$conf['reg_money']?$conf['reg_money']:'0.00';
		$vip = null;
        if($conf['user_vip']>0){
            $vip= date("Y-m-d H:i:s", strtotime("+{$conf['user_vip']}days")); //计算会员到期时间
        }
        $sql = "INSERT INTO `pay_user` (`user`,`pass`,`pid`,`key`,`qq`,`email`,`phone`,`money`,`addtime`) VALUES ('{$user}','{$pass}','{$pid}','{$key}','{$qq}','{$email}','{$phone}','{$money}','{$date}')";
        if(executeSQLQuery($sql,'insert')){
            if($vip){
                $sql = "update `pay_user` set `wxpay_free_vip_time`='{$vip}',`qqpay_free_vip_time`='{$vip}',`alipay_free_vip_time`='{$vip}' where pid='{$pid}'";
                executeSQLQuery($sql,'update');
            }
             $sql ="update `pay_regcode` set `status` ='1' where `id`='{$row['id']}'";
             executeSQLQuery($sql,'update');
             send_regsucc($user,$postdata['pass'],$email,true);
            send_regsucc($user,$postdata['pass'],$phone);
            createLog($pid,'申请商户成功',http_ip());
            return true;
        }
    }
     return false;
}

function User_quota_notify_url($out_trade_no){
    global $conf;
    $getdata = input("get");
    $sql = "select * from `pay_insideorder` where `order`='{$out_trade_no}'  limit 1";
    $row = executeSQLQuery($sql);
    if($row && $row['status'] == 0){
        $pid     = end(explode("|", $row['beizhu']));
        if ($getdata['trade_status'] == 'TRADE_SUCCESS'){
            $money =  $getdata['money'] * $conf['ed_money'];
            $sql = "update `pay_user` set `money`=`money`+'{$money}' where pid='{$pid}'";
            executeSQLQuery($sql,'update');
            $sql = "update `pay_insideorder` set `status`='1' where id='{$row['id']}'";
            executeSQLQuery($sql,'update');
            createLog($pid,'充值额度成功！',http_ip());
            return true;
        }
    }
    return false;
}

function User_payvip_notify_url($out_trade_no){
    global $conf,$date;
    $getdata = input("get");
    $sql = "select * from `pay_insideorder` where `order`='{$out_trade_no}'  limit 1";
    $row = executeSQLQuery($sql);
    
    if($row && $row['status'] == 0){
        $pid     = $row['pid'];
        if ($getdata['trade_status'] == 'TRADE_SUCCESS'){
            
            $type  = $row['beizhu'];
            $money =  $conf[$type.'_free_vip_money'];
            if($money == $getdata['money']){
                $sql = "select * from `pay_user` where `pid`='{$pid}'  limit 1";
                $userrow = executeSQLQuery($sql);
                if($userrow){
                    $vip = $type.'_free_vip_time';
                    $vipTime = strtotime($userrow[$vip]);
                    $currentDate = strtotime($date);
                    if ($vipTime < $currentDate){
                        $newVipTime = strtotime("+1 month", $currentDate);
                    }else{
                        $newVipTime = strtotime("+1 month", $vipTime);
                    }
                    $newVipTimeStr = date("Y-m-d H:i:s", $newVipTime);
                    $where = '';
                    $where .= $vip." = '".$newVipTimeStr."', ";
                    if (!empty($where)){
                        $where = rtrim($where, ", ");
                        $sql = "UPDATE pay_user SET $where WHERE pid='$pid'";
                        executeSQLQuery($sql, 'update');
                        $sql = "update `pay_insideorder` set `status`='1' where id='{$row['id']}'";
                        executeSQLQuery($sql,'update');
                        createLog($pid,'充值额度成功！',http_ip());
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

function User_setmenu_notify_url($out_trade_no){
    global $date;
    $getdata = input("get");
    $sql = "select * from `pay_insideorder` where `order`='{$out_trade_no}'  limit 1";
    $row = executeSQLQuery($sql);
    if($row && $row['status'] == 0){
        $pid     = $row['pid'];
        if ($getdata['trade_status'] == 'TRADE_SUCCESS'){
            $id    = $row['beizhu'];
            $money = $row['money'];
            if($money == $getdata['money']){
                $sql = "select * from `pay_package` where `id`='{$id}'  limit 1";
                $packrow = executeSQLQuery($sql);
                $sql = "select * from `pay_user` where `pid`='{$pid}'  limit 1";
                $userrow = executeSQLQuery($sql);
                if($packrow && $userrow){
                    $where = '';
                    $type = array(
                        '1'=>'alipay',
                        '2'=>'wxpay',
                        '3'=>'qqpay',
                    );
                    if ($packrow['days']) {
                        $valueSets = array(); 
                        foreach ($type as $t) {
                            $vip = $t . '_free_vip_time'; // 动态构建列名，例如：alipay_free_vip_time
                             
                            $vipTime = strtotime($userrow[$vip]);
                            $currentDate = strtotime($date);
                            if ($vipTime < $currentDate) {
                                $newVipTime = strtotime("+{$packrow['days']} days", $currentDate);
                            } else {
                                $newVipTime = strtotime("+{$packrow['days']} days", $vipTime);
                            }
                           
                            $formattedDate = date('Y-m-d H:i:s', $newVipTime);
                            $valueSets[] = "$vip='$formattedDate'";
                            
                        }
                       
                        $where = implode(' ,', $valueSets).' , ';
                         
                    }
                   
                    if(is_numeric($packrow['quota']) && $packrow['quota'] > 0){
                        $where.='`money`=`money`+"'.$packrow['quota'].'" , ';
                    }
                    if (!empty($where)){
                        $where = rtrim($where, ", ");
                        $sql = "UPDATE pay_user SET $where WHERE pid='$pid'";
                        executeSQLQuery($sql, 'update');
                        $sql = "update `pay_insideorder` set `status`='1' where id='{$row['id']}'";
                        executeSQLQuery($sql,'update');
                        createLog($pid,'购买套餐成功！',http_ip());
                        return true;
                    }
                }
            }
        }
    }
    return false;
}


function User_quotapackage_notify_url($out_trade_no){
    global $date;
    $getdata = input("get");
    $sql = "select * from `pay_insideorder` where `order`='{$out_trade_no}'  limit 1";
    $row = executeSQLQuery($sql);
    if($row && $row['status'] == 0){
        $pid     = $row['pid'];
        if ($getdata['trade_status'] == 'TRADE_SUCCESS'){
            $money = $row['beizhu'];
            if($row['money'] == $getdata['money']){
                $sql = "select * from `pay_user` where `pid`='{$pid}'  limit 1";
                $userrow = executeSQLQuery($sql);
                if($userrow){
                    $sql = "UPDATE pay_user SET `money` = `money`+'{$money}' WHERE pid='$pid'";
                    executeSQLQuery($sql, 'update');
                    $sql = "update `pay_insideorder` set `status`='1' where id='{$row['id']}'";
                    executeSQLQuery($sql,'update');
                     createLog($pid,'购买余额套餐成功！',http_ip());
                     return true;
                }
            }
        }
    }
    return false;
    
}



?>